var ArticleModel = require("../models/article");
var moment = require('moment')
var fs = require('fs');
const { send } = require('process');
module.exports.list = async function (req, res, next) {
  var page = req.query.page || 1
  var data = {
    total: 0,
    curPage: page,
    list: [],
    pageSize: 5
  }

  //1.查询所有文章
  ArticleModel.find((err, docs) => {
    data.total = docs.length
    //2.查询当前页的文章列表
    ArticleModel.find({}, null,
      {
        skip: (page - 1) * data.pageSize,
        limit: data.pageSize,
        sort: { _id: -1 }
      },
      (err, docs2) => {
        if (docs2.lenth == 0) {
          res.redirect('/page=' + ((page - 1) || 1))
        } else {
          docs2.map((ele, index) => {
            ele['time'] = moment(ele.id).format('YYYY-MM-DD HH:mm:ss')
          })
          data.list = docs2
          res.send(200, { data })
        }
      }
    )
  })
}

module.exports.write = async function (req, res, next) {
  var username = req.query.userInfo.username
  var id = parseInt(req.query.id)
  var page = req.query.page
  var item = {
    title: '',
    content: ''
  }
  if (id) {
    ArticleModel.findOne({ id: id }, (err, docs) => {
      if (err) {} else {

        item = docs

        res.send(200, { username: username, form: item })
      }
    })
  } else {
    res.send(200, { username: username, form: item })
  }
}

module.exports.add = async (req, res, next) => {
  var id = parseInt(req.body.id)
  if (id) {
    var page = req.body.page
    var title = req.body.title
    var content = req.body.content
    var category = req.body.category
    ArticleModel.updateOne({ id: id }, {
      $set: {
        title: title,
        content: content,
        category: category
      }
    }, (err, ret) => {
      if (err) {
        console.log("修改失败", err);
      } else {
        console.log("修改成功");
        res.send(200)
      }
    })
  } else {
    var data = {
      title: req.body.title,
      content: req.body.content,
      id: Date.now(),
      username: req.query.userInfo.username,
      category: req.body.category
    }
    console.log(data);
    ArticleModel.create(data, (err) => {
      if (err) {
        console.log('文件发布失败', err);
        res.send(err)
      } else {
        console.log("添加成功");
        res.send(200)
      }
    })
  }
}

module.exports.delete = async (req, res, next) => {
  var id = parseInt(req.query.id)
  var page = req.query.page
  ArticleModel.deleteOne({ id: id }, (err, ret) => {
    if (err) {
      console.log('删除失败');
    } else {
      console.log("删除成功");
      res.send(200)
    }
  })
}

module.exports.upload = async (req, res, next) => {
  var file = req.body.file
  var base64Data = file.miniurl.replace(/^data:image\/\w+;base64,/, "");
  // 返回一个被 string 的值初始化的新的 Buffer 实例, 原始二进制数据存储在 Buffer 类的实例中， 一个 Buffer 类似于一个整数数组， 但它对应于 V8 堆内存之外的一块原始内存。
  var dataBuffer = Buffer.from(base64Data, 'base64');
  // console.log(file._name);
  fs.writeFile("./public/uploads/" + file._name, dataBuffer, function (err) {
    if (err) {
      res.send(err);
    } else {
      var newPath = "http://192.168.207.196:3000/uploads/" + file._name
      res.send(200, { newPath });
    }
  })
}

module.exports.detail = async function (req, res, next) {
  var id = parseInt(req.query.id)
  var username = req.query.userInfo.username
  console.log(id);
  ArticleModel.findOne({ id: id }, (err, docs) => {
    if (err) {} else {
      var item = docs
      console.log(item);
      item['time'] = moment(item.id).format('YYYY-MM-DD HH:mm:ss')
      res.send(200, { form: item, username: username })
    }
  })
}